package com.controller;


import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.YishiEntity;
import com.entity.YonghuEntity;
import com.entity.view.YishiView;
import com.entity.view.YonghuView;
import com.service.TokenService;
import com.service.YishiService;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/yishi")
public class YishiController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private YishiService yishiService;



    /**
     * 登录
     */
    @IgnoreAuth
    @RequestMapping(value = "/login")
    public R login(String username, String password, String captcha, HttpServletRequest request) {
        YishiEntity u = yishiService.selectOne(new EntityWrapper<YishiEntity>().eq("yonghuming", username));
        if(u==null || !u.getMima().equals(password)) {
            return R.error("账号或密码不正确");
        }

        String token = tokenService.generateToken(u.getId(), username,"yishi",  "用户" );
        return R.ok().put("token", token);
    }


    /**
     * 注册
     */
    @IgnoreAuth
    @RequestMapping("/register")
    public R register(@RequestBody YishiEntity yonghu){
        YishiEntity u = yishiService.selectOne(new EntityWrapper<YishiEntity>().eq("yonghuming", yonghu.getYonghuming()));
        if(u!=null) {
            return R.error("注册用户已存在");
        }
        Long uId = new Date().getTime();
        yonghu.setId(uId);
        yishiService.insert(yonghu);
        return R.ok();
    }


    /**
     * 退出
     */
    @RequestMapping("/logout")
    public R logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.ok("退出成功");
    }

    /**
     * 获取用户的session用户信息
     */
    @RequestMapping("/session")
    public R getCurrUser(HttpServletRequest request){
        Long id = (Long)request.getSession().getAttribute("userId");
        YishiEntity u = yishiService.selectById(id);
        return R.ok().put("data", u);
    }

    /**
     * 密码重置
     */
    @IgnoreAuth
    @RequestMapping(value = "/resetPass")
    public R resetPass(String username, HttpServletRequest request){
        YishiEntity u = yishiService.selectOne(new EntityWrapper<YishiEntity>().eq("yonghuming", username));
        if(u==null) {
            return R.error("账号不存在");
        }
        u.setMima("123456");
        yishiService.updateById(u);
        return R.ok("密码已重置为：123456");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody YishiEntity yonghu, HttpServletRequest request){
        //ValidatorUtils.validateEntity(yonghu);
        yishiService.updateById(yonghu);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        yishiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( YishiEntity yonghu){
        EntityWrapper<YishiEntity> ew = new EntityWrapper<YishiEntity>();
        ew.allEq(MPUtil.allEQMapPre( yonghu, "yishi"));
        return R.ok().put("data", yishiService.selectListView(ew));
    }

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,YishiEntity yonghu,
                  HttpServletRequest request){
        EntityWrapper<YishiEntity> ew = new EntityWrapper<YishiEntity>();

        PageUtils page = yishiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        YishiEntity yonghu = yishiService.selectById(id);
        return R.ok().put("data", yonghu);
    }


    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody YishiEntity yonghu, HttpServletRequest request){
        yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(yonghu);
        YishiEntity u = yishiService.selectOne(new EntityWrapper<YishiEntity>().eq("yonghuming", yonghu.getYonghuming()));
        if(u!=null) {
            return R.error("用户已存在");
        }
        yonghu.setId(new Date().getTime());
        yishiService.insert(yonghu);
        return R.ok();
    }




}
